package com.vecspace.dayu.code.httphandler;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;

import com.vecspace.dayu.code.model.DataColumnsVO;
import com.vecspace.dayu.code.pojo.DataColumn;
import com.vecspace.dayu.code.service.DataColumnService;
import com.vecspace.dicttable.CodeItem;
import com.vecspace.dicttable.DictAll;
import com.vecspace.security.model.LoginContext;
import com.vecspace.web.servlet.model.BaseController;
import com.vecspace.web.servlet.model.HController;
import com.vecspace.web.servlet.model.Param;
import com.vecspace.web.servlet.model.Path;

/** DAYU-字段定义 httphandler
 * 在handler中进行与页面相关的处理，比如把数据转换为HTML格式等。
 * @author wanghua
 * @reviewer
 * @template wanghua
*/
@HController("dayucode_dataColumnHandler")
public class DataColumnHandler extends BaseController {

	/**根据ID查询 DAYU-字段定义
	 * @return DataColumn
	 * @throws Exception
	 * @author wanghua
	 */
	@Path("getDataColumn")
	public DataColumn getDataColumn(@Param("id") Long id) throws Exception {
		return dataColumnService.getDataColumn(id);
	}
	

	/**根据表id获取所有字段
	 * 
	 * @param datatableId
	 * @return
	 * @throws Exception 
	 * @author wanghua
	 */
	@Path("getDataColumnsVO")
	public DataColumnsVO getDataColumns(@Param("datatableId")Long datatableId) throws Exception {
		DataColumnsVO vo = new DataColumnsVO();
		List<DataColumn> columns = dataColumnService.getDataColumns(datatableId);
		vo.setColumns(columns);
		List<CodeItem> columnTypes = DictAll.getItems(DataColumn.ColumnTypeCode);
		vo.setColumnTypes(columnTypes);
		return vo;
	}

	/** 插入或修改 DAYU-字段定义 
	 * @param item	要保存的对象
	 * @return void
	 * @throws Exception
	 * @author wanghua
	 */
	@Path("saveDataColumn")
	public DataColumn saveDataColumn(@Param("item") DataColumn item) throws Exception {
		if (item == null){
			return null;
		}
		/*** 对item对象进行校验，如必填项，格式，逻辑关系等 ***/
		String loginUserId = LoginContext.getLoginId();
		return dataColumnService.saveViewDataColumn(item, loginUserId);
	}
	
	/**更新单个字段的值
	 * 
	 * @param id
	 * @param propName
	 * @param value
	 * @return
	 * @throws Exception 
	 * @author wanghua
	 */
	@Path("updateProp")
	public DataColumn updateProp(@Param("id")Long id, @Param("propName")String propName, @Param("value")String value) throws Exception {
		dataColumnService.updateProp(id, propName, value);
		return dataColumnService.getDataColumn(id);
	}

	/** 根据ID删除 DAYU-字段定义 
	 * @param id	主键值
	 * @return void
	 * @throws Exception
	 * @author wanghua
	 */
	@Path("deleteDataColumn")
	public void deleteDataColumn(@Param("id") Long id) throws Exception {
		dataColumnService.deleteDataColumn(id);
	}
	/************* property ********************/
	@Autowired
	private DataColumnService dataColumnService;

}
